package com.dragon.client.controller.customer;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.client.config.annotation.Log;
import com.dragon.common.model.Insert;
import com.dragon.common.model.ResponseData;
import com.dragon.common.model.Update;
import com.dragon.logical.service.customer.IGoodsService;
import com.dragon.repository.domain.dto.GoodsDTO;
import com.dragon.repository.domain.vo.GoodsVO;
import com.dragon.repository.entity.Goods;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 产品 表 前端控制器
 * </p>
 *
 * @author sy
 * @since 2023-06-30
 */
@RestController
@RequestMapping("goods")
@Api(tags = "产品")
public class GoodsController {

    @Autowired
    IGoodsService service;

    @GetMapping("list")
    @ApiOperation("产品列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", required = true  ,type = "int"),
            @ApiImplicitParam(name = "limit", value = "条数", required = true, type = "int"),
            @ApiImplicitParam(name = "search", value = "搜索条件", required = false, type = "string")
    })
    public ResponseData<List<GoodsDTO>> goodsList(@RequestParam Integer page, @RequestParam Integer limit, @RequestParam(required = false) String search){
        Page<Goods> goodsPage = service.goodsList(page, limit, search);
        return ResponseData.success(GoodsDTO.convertList(goodsPage.getRecords()), goodsPage.getTotal());
    }

    @PostMapping("add")
    @ApiOperation("添加产品")
    @Log
    public ResponseData addGoods( @RequestBody @Validated(Insert.class) GoodsVO vo){
        return ResponseData.success( service.addGoods(GoodsVO.convert(vo)) );
    }

    @PostMapping("update")
    @ApiOperation("更新产品信息")
    @Log
    public ResponseData updateGoods(@RequestBody @Validated(Update.class) GoodsVO vo ){
        service.updateGoods(GoodsVO.convert(vo));
        return ResponseData.success();
    }


    @PostMapping("delete")
    @ApiOperation("删除产品")
    public ResponseData deleteGoods( @ApiParam(name = "id", value = "产品id",required = true, type = "int") @RequestParam Integer id){
        service.deleteGoods(id);
        return ResponseData.success();
    }
}
